﻿
Imports std
Imports xmlctrl
Imports usshare

Public Class txtapi
    Inherits servelib

    Public Const _pathidentity As String = network.pathSeperator + "txtapi"

    Private Shared Function generateTextOutput(ByVal context As httpContext, ByVal s As String) As xmlcontrol
        Dim rtn As xmlcontrol = Nothing
        rtn = context.createControl(New xmltext(xmlcontrol.textEncodingEnum.none))
        rtn.text = s
        setHttpHeaders(context)
        Return rtn
    End Function

    Private Shared Function generateTextOutput(ByVal s As String) As xmlcontrol
        Return generateTextOutput(httpServer2.currentContext(), s)
    End Function

    Private Shared Sub setHttpHeaders(ByVal context As httpContext)
        context.setcontentType(network.http.contentType.textplainUtf8)
        context.setcontentEncoding(encoding.utf8())
    End Sub

    Protected Overrides Function have(ByVal context As httpContext, ByRef control As xmlcontrol, _
                                      ByVal haveResult As Boolean, ByVal url As String) As Boolean
        control = generateTextOutput(context, Convert.ToString(haveResult))
        Return True
    End Function

    Protected Overrides Function homepage(ByVal context As httpContext, ByRef control As xmlcontrol) As Boolean
        Dim txt As String = Nothing
        txt = pathidentity() + urlUtils.combineQuery(query.url, "THE_URL_YOU_WANT_TO_SHORT", _
                                                     query.key, "THE_SHORTENURL_YOU_PREFER", _
                                                     query.action, "ACTION_VERB")
        control = generateTextOutput(context, txt)
        Return True
    End Function

    Protected Overrides Function jump(ByVal context As httpContext, ByRef control As xmlcontrol, _
                                      ByVal key2urlResult As Boolean, _
                                      ByVal key As String, ByVal url As String, ByVal delay As Int64) As Boolean
        Return preview(context, control, key2urlResult, key, url)
    End Function

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Protected Overrides Function preview(ByVal context As httpContext, ByRef control As xmlcontrol, _
                                         ByVal key2urlResult As Boolean, _
                                         ByVal key As String, ByVal url As String) As Boolean
        control = generateTextOutput(url)
        Return True
    End Function

    Protected Overrides Function submit(ByVal context As httpContext, ByRef control As xmlcontrol, _
                                        ByVal submitResult As submitResult, _
                                        ByVal key As String, ByVal url As String) As Boolean
        Dim txt As String = Nothing
        Select Case submitResult
            Case submitResult.success
                txt = urlgenerator.jumpfull(key)
            Case submitResult.existingKey
                txt = submitResponse.existingKey
            Case submitResult.internalError
                txt = submitResponse.internalError
            Case submitResult.invalidKey
                txt = submitResponse.invalidKey
            Case Else
                'assert here, if code runs to here, something special happened.
                assert(False, "should cover all possibilities.")
        End Select
        control = generateTextOutput(txt)
        Return True
    End Function

    Protected Overrides Function urlkeys(ByVal context As httpContext, ByRef control As xmlcontrol, _
                                         ByVal url2keysResult As Boolean, _
                                         ByVal url As String, ByVal keys As [set](Of String)) As Boolean
        Dim txt As String = Nothing
        If url2keysResult AndAlso Not keys Is Nothing AndAlso keys.size > 0 Then
            Dim it As [set](Of String).iterator = Nothing
            it = keys.begin()
            While it <> keys.end()
                txt += (+it) + newline.incode()
                it += 1
            End While
        End If
        control = generateTextOutput(txt)
        Return True
    End Function
End Class
